<template>
  <div class="app-container">
    <el-card v-show="showSearch" class="mb10" shadow="hover">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="120px" size="small">
        <el-form-item label="会员ID" prop="params.memberId">
          <el-input
            v-model="queryParams.params.memberId"
            clearable
            placeholder="请输入会员ID"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="油站" prop="params.stationId">
          <el-select v-model="queryParams.params.stationId" clearable placeholder="请选择油站">
            <el-option
              v-for="item in stationList"
              :key="item.id"
              :label="item.fullName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道ID" prop="params.channelId">
          <el-input
            v-model="queryParams.params.channelId"
            clearable
            placeholder="请输入渠道ID"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            :default-time="['00:00:00', '23:59:59']"
            end-placeholder="结束日期"
            range-separator="-"
            start-placeholder="开始日期"
            style="width: 100%"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item class="ml10">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="hover">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['member:points:record:remove']"
            :disabled="multiple"
            icon="el-icon-delete"
            plain
            size="mini"
            type="danger"
            @click="handleDelete"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['member:points:record:export']"
            icon="el-icon-download"
            plain
            size="mini"
            type="warning"
            @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="pointsRecordList" border @sort-change="handleSortChange"
                @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column align="center" label="会员ID" prop="memberId" show-overflow-tooltip/>
        <el-table-column align="center" label="油站" prop="stationName" show-overflow-tooltip/>
        <el-table-column align="center" label="渠道" prop="channel" show-overflow-tooltip/>
        <el-table-column align="center" label="渠道ID" prop="channelId" show-overflow-tooltip/>
        <el-table-column align="center" label="操作积分" prop="operationPoints" show-overflow-tooltip/>
        <el-table-column align="center" label="有效积分" prop="effectivePoints" show-overflow-tooltip/>
        <el-table-column align="center" label="当前积分" prop="currentPoints" show-overflow-tooltip/>
        <el-table-column align="center" label="操作类型" prop="operationType" show-overflow-tooltip/>
        <el-table-column align="center" label="扣减渠道ID" prop="sourceChannelId" show-overflow-tooltip/>
        <el-table-column align="center" label="过期时间" prop="expireDate" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.expireDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建人名称" prop="creatorName" show-overflow-tooltip/>
        <el-table-column align="center" label="创建时间" prop="createDate" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ formatDateTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注信息" prop="remark" show-overflow-tooltip/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template #default="scope">
            <el-tooltip content="删除" placement="top">
              <el-button
                v-hasPermi="['member:points:record:remove']"
                icon="el-icon-delete"
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"/>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {listPointsRecord} from "@/api/member/pointsRecord";
import {formatDateTime, nowCnFormatDateTime} from "@/utils/date";
import {selectList} from "@/api/base/station";

export default {
  name: "PointsRecord",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员积分记录表格数据
      pointsRecordList: [],
      // 油站信息
      stationList: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sortName: 'id',
        sortOrder: 'desc',
        params: {
          mchId: undefined,
          memberId: undefined,
          stationId: undefined,
          channel: undefined,
          channelId: undefined,
          operationPoints: undefined,
          effectivePoints: undefined,
          currentPoints: undefined,
          operationType: undefined,
          sourceChannelId: undefined,
          expireDate: undefined,
          creatorId: undefined,
          creatorName: undefined,
          createDate: undefined,
          modifierId: undefined,
          modifierName: undefined,
          modifiedDate: undefined,
          active: undefined,
        }
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getStationList();
  },
  methods: {
    formatDateTime,
    /** 查询会员积分记录列表 */
    getList() {
      this.loading = true;
      listPointsRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.pointsRecordList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getStationList() {
      selectList().then(res => {
        this.stationList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        mchId: undefined,
        memberId: undefined,
        stationId: undefined,
        channel: undefined,
        channelId: undefined,
        operationPoints: undefined,
        effectivePoints: undefined,
        currentPoints: undefined,
        operationType: undefined,
        sourceChannelId: undefined,
        expireDate: undefined,
        creatorId: undefined,
        creatorName: undefined,
        createDate: undefined,
        modifierId: undefined,
        modifierName: undefined,
        modifiedDate: undefined,
        active: undefined,
        remark: undefined,
        version: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleSortChange(data) {
      console.log('handleSortChange')
      console.log(data)
      if (data.order) {
        if (data.order.indexOf('asc') > -1) {
          this.queryParams.sortOrder = 'asc'
        } else {
          this.queryParams.sortOrder = 'desc'
        }
        this.queryParams.sortName = data.prop
      }
      console.log(this.queryParams)
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员积分记录编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delPointsRecord(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pointsRecord/export', {
        ...this.queryParams
      }, `pointsRecord_${nowCnFormatDateTime()}.xlsx`)
    }
  }
}

</script>

<style>
.el-form .el-select {
  width: 100%;
}
</style>
